package game
{
	import controller.FriendRobotController;
	import controller.MainWeaponController;
	import controller.MapCameraController;
	import controller.MyRobotController;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.Responder;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import gameObject.BattleMap;
	import gameObject.Robot;
	import gameObject.WeaponMain;
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.MapResource;
	
	import net.P2p;
	
	import ui.component.Button;
	import ui.component.TextInput;
	
	public class BattleWorld extends Sprite
	{
		private var _floor:GameMesh;
		
		private var _map:BattleMap;
		
		private var _players:Array;
		private var _testP2PUI:Sprite;
		private var _btnBeServer:Button;
		private var _pidInput:TextInput;
		private var _btnConnectTo:Button;
		
		private var _loadedPlayers:int = 0;
		
		/**
		 * @param mapID 地图编号
		 * @param Players 进入地图玩家的uid组成的数组
		 */		
		public function BattleWorld(mapID:int,level:int,Players:Array)
		{
			setupTestUI();
			
			_map = new BattleMap(mapID,level);
			Global.gameView.scene.addChild(_map);
			
			_players = Players;
			
			_map.addEventListener(Event.COMPLETE,setup);
		}
		
		private function setup(e:Event):void
		{
			_map.removeEventListener(Event.COMPLETE,setup);
			
//			Global.stage.addChild(CollisionWorld.collisionMap);
//			CollisionWorld.collisionMap.alpha = 0.5;
			
			CollisionWorld.collObjs = new Dictionary;
			for each(var i:int in _players)
			{
				var robot:Robot = new Robot(i);
				robot.addEventListener(Event.COMPLETE,setupRobot);
				
				P2pWorld.robots[i]=robot;
				_map.addChild(robot);
			}
		}
		
		private function setupRobot(e:Event):void
		{
			var robot:Robot = e.currentTarget as Robot;
			robot.removeEventListener(Event.COMPLETE,setupRobot);
			
			robot.x = 4*_players.indexOf(robot.uid)*robot.hitArea.radius;
			
			var mapRes:MapResource = _map.resource as MapResource;
			if(robot.uid==Global.uid)//为自己的机体安装控制器
			{
				//角色主控制器
				var robCon:MyRobotController = new MyRobotController(mapRes);
				robCon.x = 4*_players.indexOf(robot.uid)*robot.hitArea.radius;
				robot.controller = robCon;
				
				//主武器控制器
				for each(var mainWep:WeaponMain in robot.mainWeapons)mainWep.controller = new MainWeaponController;
				
				//让相机跟着自己的角色
				var cameraCon:MapCameraController = new MapCameraController;
				_map.controller = cameraCon;
				cameraCon.followObject(robot);
				//cameraCon.mouseRotateCamera();
				
			}else//为其它玩家的机体安装控制器
			{
				var fCon:FriendRobotController = new FriendRobotController;
				robot.controller = fCon;
			}
			
			CollisionWorld.collObjs[robot.uid] = robot;
			
			//Global.stage.addChild(robot.hitArea);
			robot.hitArea.alpha = 0.5;
			robot.hitArea.radius *= mapRes.collisionMap.width/mapRes.width;
			
			_loadedPlayers++;
		}
		
		///////////////////////////////////////////////testP2P
		private function setupTestUI():void
		{
			_testP2PUI = new Sprite;
			addChild(_testP2PUI);
			
			var bmd:BitmapData = new BitmapData(40,20,false,0xffff44);
			_btnBeServer = new Button(bmd,bmd,bmd,callBeServer);
			_testP2PUI.addChild(_btnBeServer);
			_btnBeServer.x = 30;
			_btnBeServer.y = 10;
			
			_pidInput = new TextInput(16,32,20,3,false,"0-9");
			_testP2PUI.addChild(_pidInput);
			_pidInput.x = 80;
			_pidInput.y = 10;
			_pidInput.border = true;
			
			var pidText:TextField = new TextField;
			pidText.mouseEnabled = false;
			pidText.defaultTextFormat = _pidInput.defaultTextFormat;
			pidText.text = String(Global.uid);
			_testP2PUI.addChild(pidText);
			pidText.x = -30
			pidText.y = 10;
			
			bmd = new BitmapData(40,20,false,0x22ff22);
			_btnConnectTo = new Button(bmd,bmd,bmd,callConnectTo);
			_testP2PUI.addChild(_btnConnectTo);
			_btnConnectTo.x = 120;
			_btnConnectTo.y = 10;
			
			bmd = new BitmapData(40,20,false,0xff0000);
			var btnQuit:Button = new Button(bmd,bmd,bmd,callQuit);
			_testP2PUI.addChild(btnQuit);
			btnQuit.x = 180;
			btnQuit.y = 10;
		}
		
		private function callQuit():void
		{
			if(parent)parent.removeChild(this);
			if(_map && _map.parent)_map.parent.removeChild(_map);
			
			
			Global.stage.addChild(new Citadel);
			
			Global.gc();
		}
		
		private function callBeServer():void
		{
			Global.P2PServer.type = P2p.SERVER;
		}
		
		private function callConnectTo():void
		{
			var sid:int = int(_pidInput.text);
			Global.phpServer.call("userServer.getPeerID", new Responder(gotPeerID),sid);
		}
		
		private function gotPeerID(obj:Object):void
		{
			var peerID:String = String(obj);
			
			Global.P2PServer.type = P2p.CLIENT;
			Global.P2PServer.connectToServer(peerID);
		}
	}
}